//
//  ZCCollectionVC.m
//  zctx
//
//  Created by apple on 16/1/4.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCCollectionVC.h"
#import "ZCCollectionCell.h"
#import "ZCinformationVC.h"
@interface ZCCollectionVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;

@property BOOL isnothing;
@end

@implementation ZCCollectionVC
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCCollectionVC"];
    [MTA trackPageViewBegin:@"ZCCollectionVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCCollectionVC"];
    [MTA trackPageViewEnd:@"ZCCollectionVC"];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

-(void)updateRequest{
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus >0) {
        self.bannerView.hidden = YES;
    }
    [self requestData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //AF 开启监听网络状态
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    // Do any additional setup after loading the view.
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationItem.title = @"收藏的文章";
    self.isnothing = NO;
    self.dataArray = [[NSMutableArray alloc]init];
    [self initnavegation];
    
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        [self requestData];
//    });
    
    [self initTableView]; //初始化UITableView
    [self requestData];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCCollectionCell" bundle:nil] forCellReuseIdentifier:@"collectiomcell"];
}


#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
   
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)requestData{
    [GMDCircleLoader setOnView:self.view withTitle:@"努力加载中..." animated:YES];
    [NetworkManager requestIndexGetCollectionNewsmid:UserModel.mid password:UserModel.password pagecount:@"0" success:^(NSDictionary *dictResponse) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        if ([[dictResponse objectForKey:@"content"] isKindOfClass:[NSArray class]]) {
            NSArray *array = (NSArray *)[dictResponse objectForKey:@"content"];
            self.dataArray = [array mutableCopy];
            if (self.dataArray.count>0) {
                self.isnothing = NO;
            }else{
                self.isnothing = YES;
            }
            [self.tableView reloadData];
        }else{
            self.isnothing = YES;
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        [GMDCircleLoader hideFromView:self.view animated:YES];
        if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == 0) {
            self.bannerView.hidden = NO;
        }
    }];
}

-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.sectionFooterHeight = 0.0001;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

#pragma mark UITableViewDataSource,UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndentifier = @"collectiomcell";
    ZCCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[ZCCollectionCell alloc]initWithStyle:
                UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    cell.dict = (NSDictionary *)self.dataArray[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.000001;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
    NSDictionary *dict = (NSDictionary *)self.dataArray[indexPath.row];
    NSString *strid = [NSString stringWithFormat:@"%@",[dict objectForKey:@"for_id"]];
    NSString *strurl = [NSString stringWithFormat:@"%@",[dict objectForKey:@"url"]];
    NSString *strnews_type = [NSString stringWithFormat:@"%@",[dict objectForKey:@"news_type"]];
    ZCinformationVC *infoVC = [[ZCinformationVC alloc]init];
    infoVC.hidesBottomBarWhenPushed = YES;
    infoVC.urlID = strid;
    infoVC.strURL = strurl;
    if ([strnews_type isEqualToString:@"3"]) {
        infoVC.isweekly = @"1";
    }
    [self.navigationController pushViewController:infoVC animated:YES];

}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (self.isnothing==NO) {
        UIView *footView = [[UIView alloc]init];
        footView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
        return footView;
    }else{
        
        UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH,400)];
        footView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
        
        UIImageView*redImg = [[UIImageView alloc]init];
        [footView addSubview:redImg];
        [redImg makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(footView.top).with.offset(0.255 * SCREEN_HEIGHT);
            make.centerX.equalTo (footView.centerX).with.offset(0);
            make.height.mas_equalTo(100);
            make.width.mas_equalTo(100);
        }];
        [redImg setImage:[UIImage imageNamed:@"me_collection_no"]];
        
        UILabel*redLable = [[UILabel alloc]init];
        [footView addSubview:redLable];
        [redLable makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(redImg.bottom).with.offset(23);
            make.centerX.equalTo (footView.centerX).with.offset(0);
            make.height.mas_equalTo(21);
            make.width.mas_equalTo(150);
        }];
        redLable.textAlignment = NSTextAlignmentCenter;
        redLable.text = @"无收藏的文章";
        redLable.textColor = [UIColor colorWithHexString:@"#d2d2d2"];
        redLable.font = [UIFont systemFontOfSize:16];
        return footView;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.isnothing ==NO) {
        return 0.001;
    }else{
        return 400;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
